//
//  WBVisitorView.swift
//  weiboD
//
//  Created by gao2015 on 2016/12/2.
//  Copyright © 2016年 gao2015. All rights reserved.
//

import UIKit

class WBVisitorView: UIView {

    
    
    //定义闭包,闭包类型是(() -> ())
    var callBack : (() -> ())?
    
    //懒加载控件
        //旋转视图
    private lazy var circleView:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    
        //遮罩图
    private lazy var maskImageView:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
    
        //小房子
    private lazy var iconImageView:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    
        //信息
    private lazy var messageLabel:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 13)
        label.textAlignment = .center
        label.numberOfLines = 0;
        label.textColor = UIColor.darkGray
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        return label
    }()
    
        //注册和登录按钮
    private lazy var registBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", for: .normal)
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        //点击事件
        btn.addTarget(self, action: #selector(registAction), for: .touchUpInside)
        btn.adjustsImageWhenHighlighted = false

        return btn
    }()
    
    //登录按钮
    private lazy var LoginBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", for: .normal)
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        btn.adjustsImageWhenHighlighted = false
        //点击事件
        btn.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        return btn
    }()
    
    //注册功能
    func registAction(){
        //注册
        print("注册")
        //执行闭包
        callBack?()
        
        
    
    }
    //登录功能
    func loginAction(){
       print("登录")
       callBack?()
        
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI()  {
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        addSubview(circleView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registBtn)
        addSubview(LoginBtn)
        
        //布局
        circleView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(circleView)
        }

        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.top.equalTo(circleView.snp_bottom)
            make.width.equalTo(224)
            make.centerX.equalTo(circleView)
        }
        
        registBtn.snp_makeConstraints { (make) in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 80, height: 40))
        }
        
        LoginBtn.snp_makeConstraints { (make) in
            make.size.equalTo(registBtn)
            make.top.equalTo(registBtn)
            make.trailing.equalTo(messageLabel)
        }
    }
    
    //首页动画旋转效果
    func startAnimating(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        //失去焦点后仍然可以旋转
        animation.isRemovedOnCompletion = false
        
        //执行动画
        circleView.layer.add(animation, forKey: nil)
    
    }
    
    //修改访客视图信息
    func updateVisitorInfo(imageName:String?,message:String?){
        //若不为空则设置这2个值
        if let imgName = imageName , let msg = message{
            iconImageView.image = UIImage(named: imgName)
            messageLabel.text = msg
            circleView.isHidden = true
        }else{
            startAnimating()
        }
        
    }
    private func test() {
        //用系统自带的布局
        circleView.translatesAutoresizingMaskIntoConstraints = false;
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
    
    }

}
